<template>
  <el-dialog
    :title="title"
    :visible.sync="visible"
    width="800px"
    :modal-append-to-body="false"
    :close-on-click-modal="false"
    :append-to-body="true"
    :before-close="handleClose"
  >
    <el-form ref="ruleEdit" :model="ruleBody" :rules="rules" label-width="120px">
      <el-form-item label="提示信息" prop="Message">
        <el-input v-model="ruleBody.Message" :maxlength="50" placeholder="提示信息" />
      </el-form-item>
      <el-form-item label="提示类型" prop="IsWarn">
        <el-switch
          v-model="ruleBody.IsWarn"
          active-text="警告"
          inactive-text="错误"
        />
      </el-form-item>
      <el-form-item label="验证方式" prop="RuleType">
        <enumItem
          v-model="ruleBody.RuleType"
          :dic-key="EnumDic.ValidateRuleType"
          placeholder="验证方式"
          sys-head="form"
        />
      </el-form-item>
      <el-form-item v-if="ruleBody.RuleType ==0" label="最小值" prop="Min">
        <el-input-number
          v-model="ruleBody.Min"
          placeholder="最小值"
        />
        <el-switch
          v-model="ruleBody.MinIsEqual"
          style="margin-left: 10px;"
          active-text="<="
          inactive-text="<"
        />
        <span style="margin-left: 10px;">填写的值</span>
      </el-form-item>
      <el-form-item v-if="ruleBody.RuleType ==0" label="最大值" prop="Max">
        <el-input-number
          v-model="ruleBody.Max"
          placeholder="最大值"
        />
        <el-switch
          v-model="ruleBody.MaxIsEqual"
          style="margin-left: 10px;"
          active-text=">="
          inactive-text=">"
        />
        <span style="margin-left: 10px;">填写的值</span>
      </el-form-item>
      <el-form-item v-if="ruleBody.RuleType == 2" label="最小长度" prop="MinLen">
        <el-input-number
          v-model="ruleBody.MinLen"
          :precision="0"
          placeholder="最小长度"
        />
      </el-form-item>
      <el-form-item v-if="ruleBody.RuleType == 1" label="正则表达式" prop="Regex">
        <el-input
          v-model="ruleBody.Regex"
          maxlength="255"
          placeholder="正则表达式"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" style="text-align: center">
      <el-button type="primary" @click="save">保存</el-button>
      <el-button @click="reset">重置</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { EnumDic } from '@/customForm/config/formConfig'
export default {
  components: {
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    rule: {
      type: Object,
      default: null
    }
  },
  data() {
    const checkNum = (rule, value, callback) => {
      if (this.ruleBody.Min == null && this.ruleBody.Max == null) {
        return callback(new Error('最小数值和最大数值不能同时为空!'))
      } else if (this.ruleBody.Min != null && this.ruleBody.Max != null) {
        if (this.ruleBody.Min >= this.ruleBody.Max) {
          return callback(new Error('最小值不能大于等于最大值!'))
        }
      }
      callback()
    }
    return {
      EnumDic,
      title: '',
      ruleBody: {},
      op: 'add',
      rules: {
        Message: [
          {
            required: true,
            message: '提示信息不能为空！',
            trigger: 'blur'
          }
        ],
        RuleType: [
          {
            required: true,
            message: '验证方式不能为空！',
            trigger: 'blur'
          }
        ],
        MinLen: [
          {
            required: true,
            message: '最小长度不能为空！',
            trigger: 'blur'
          }
        ],
        Min: [
          { validator: checkNum, trigger: 'blur', key: 'min' }
        ],
        Max: [
          { validator: checkNum, trigger: 'blur', key: 'max' }
        ],
        Regex: [
          {
            required: true,
            message: '正则表达式不能为空！',
            trigger: 'blur'
          }
        ]
      },
      tableType: 0
    }
  },
  watch: {
    visible: {
      handler(val) {
        if (val) {
          this.reset()
        }
      },
      immediate: true
    }
  },
  mounted() {},
  methods: {
    reset() {
      if (this.rule == null) {
        this.op = 'add'
        this.title = '新增验证规则'
        this.ruleBody = {}
      } else {
        this.op = 'set'
        this.title = '编辑验证规则'
        this.ruleBody = this.rule
      }
    },
    handleClose() {
      this.$emit('update:visible', false)
    },
    save() {
      this.$refs['ruleEdit'].validate((valid) => {
        if (valid) {
          this.$emit('update:visible', false)
          this.$emit('update:rule', this.ruleBody)
          this.$emit('close', this.op)
        } else {
          return false
        }
      })
    }
  }
}
</script>
